<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>药品列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/bootstrap/css/bootstrap.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/table/bootstrap-table.min.css" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/statics/date/bootstrap-datetimepicker.min.css" />
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/statics/hplus/css/plugins/sweetalert/sweetalert.css" />
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/table/bootstrap-table.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/table/locale/bootstrap-table-zh-CN.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/date/Moment.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/date/bootstrap-datetimepicker.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/statics/hplus/js/plugins/sweetalert/sweetalert.min.js"></script>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-body">
        <form class="form-inline">
            <div class="form-group">
                <label>商品：</label>
                <input type="text" class="form-control" id="MName" placeholder="搜索商品">
            </div>
            <button onclick="search()" type="button" class="btn btn-default">查找</button>
            <button  type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal" id="add1">添加</button>
            <button onclick="todel()" type="button" class="btn btn-default">删除</button>
        </form>
    </div>
</div>
<table id="table"></table>
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加药品</h4>
            </div>
            <div class="modal-body">
                <form>
                    <input  type="hidden" id="id" name="id" />
                    <div class="form-group">
                        <label for="MName">药品名称：</label>
                        <input type="text" class="form-control" id="MName1">
                    </div>
                    <div class="form-group">
                        <label for="spec">药品规格：</label>
                        <input type="text" class="form-control" id="spec">

                    </div>
                    <div class="form-group">
                        <label for="Nbumber">药品数量：</label>
                        <input type="text" class="form-control" id="Nbumber"/>
                    </div>
                    <div class="form-group">
                        <label for="Dosageform">药品剂型x1：</label>
                        <input type="text" class="form-control" id="Dosageform"/>
                        <%--<option value="1">迷你包</option>
                        <option value="2">小包</option>
                        <option value="3">中包</option>
                        <option value="4">大包</option>
                        <option value="5">豪华包</option>
                        <option value="6">VIP包</option>--%>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="Termofvalidity">药品有效期：</label>
                        <input type="text" class="form-control" id="Termofvalidity"/>
                        <%--<option value="1">迷你包</option>
                        <option value="2">小包</option>
                        <option value="3">中包</option>
                        <option value="4">大包</option>
                        <option value="5">豪华包</option>
                        <option value="6">VIP包</option>--%>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="Termofvalidity">药品金额：</label>
                        <input type="text" class="form-control" id="Money"/>
                        <%--<option value="1">迷你包</option>
                        <option value="2">小包</option>
                        <option value="3">中包</option>
                        <option value="4">大包</option>
                        <option value="5">豪华包</option>
                        <option value="6">VIP包</option>--%>
                        </select>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal">关闭</button>
                <button onclick="add()" class="btn btn-primary">提交</button>
            </div>
        </div>
    </div>
</div>
</body>

<script type="text/javascript">

    $("#add1").bind("click",function () {

    $("#add").modal("show");

    })



    function todel() {
        var arr = $("#table").bootstrapTable('getSelections');
        if (arr.length == 0) {
            alert("请选择要删除的数据！");
        }
        else {

            //得到要删除行的主键

           var medicineId = arr[0]['medicineId'];
           alert(medicineId);
            del(medicineId);

        }
        /*  $.post("/t159bootstrap/a?method=del",{"id":arr},function(data){
             var r = eval("("+data+")");
             $("#dg").bootstrapTable('load',r);
         }); */
    }

    function del(id) {
        var opt = $("#table").bootstrapTable('getOptions');
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false
        }, function () {
            $.ajax({
                url:'${pageContext.request.contextPath}/medicine/delete',
                type:'post',
                data:{'medicineId': id,
                    'pageSize': opt.pageSize,
                    'pageNumber': opt.pageNumber},
                success: function (data) {
                    //绑定到table上
                    $("#table").bootstrapTable('load', data);

                    swal("删除成功！", "您已经永久删除了这条信息。", "success");

                }
            })



        });
    }



    function search() {
        var opt = $("#table").bootstrapTable('getOptions');
        var name = $("#MName").val();
        $.post("${pageContext.request.contextPath}/medicine/select", {
            'mName': name,
            'pageSize': opt.pageSize,
            'pageNumber': opt.pageNumber
        }, function (data) {
            //重新给table绑定数据
            $("#table").bootstrapTable("load", data);
        });
    }




    $(function () {

        $("#table").bootstrapTable({
            url:'${pageContext.request.contextPath}/medicine/select',
            columns: [
                {
                    checkbox:true
                },{
                    field: 'medicineId',
                    title: '药品编号'
                }, {
                    field: 'mName',
                    title: '药品名称'
                }, {
                    field: 'nbumber',
                    title: '数量'
                },  {
                    field: 'dosageform',
                    title: '药品剂型x1'
                },  {
                    field: 'money',
                    title: '药品金额'
                }
            ],
            clickToSelect:true,
            singleSelect:true,
            height:380,
//查询参数
            queryParamsType:'',

//在底部显示分页条
            pagination:true,
//启服务器端分页
            sidePagination:'server',
            pageNumber:1,
            pageSize:5,
            pageList:[5,10,15],
            queryParams:function(params){
                var temp = {
                    'pageNumber': params.pageNumber,
                    'pageSize': params.pageSize,
                   'nName':$("#MName1").val()
                } ;
                return temp ;
            }
        }) ;



    });

    function add() {
        var nName = $("#MName1").val();
        var spec = $("#spec").val();
        var nbumber = $("#Nbumber").val();
        var dosageform = $("#Dosageform").val();
        var termofvalidity = $("#Termofvalidity").val();
        var money = $("#Money").val();

        //send ajax
        $.ajax({
            url: "${pageContext.request.contextPath}/medicine/addMedicine",
            data: {"MName": nName, "Spec": spec, "Nbumber": nbumber, "Dosageform": dosageform,"Termofvalidity":termofvalidity,"Money":money},
            type: "post",
            ContentType:"application/json",
            dataType: "text",
            success: function (data) {
                alert("添加成功");

                //绑定到table上
                $("#table").bootstrapTable("refresh");

                //关闭对话框
                $('#add').modal('hide');
            },
            error:function () {
                alert("添加失败");
            }
        })
    }


</script>

</html>

